package com.cheung.leetcode.hash;

import org.junit.jupiter.api.Test;

import java.util.HashSet;
import java.util.Set;

/**
 * @BelongsProject : java-leetcode
 * @BelongsPackage : com.cheung.leetcode.hash
 * @Author :  cheungming
 * @CreateTime : 2024-05-05 09:35:58
 * @Description : 202快乐数
 * @Version : 1.0
 */
public class Code202Test {

    public boolean isHappy(int n) {
        Set<Integer> set = new HashSet<>();
        while (n != 1 && !set.contains(n)) {
            set.add(n);
            n = sum(n);
        }

        return n == 1;
    }

    public int sum(int n) {
        int sum = 0;
        while (n > 0) {
            sum += (n % 10) * (n % 10);
            n = n / 10;
        }
        return sum;
    }


    @Test
    public void test1() {
        assert isHappy(19);
    }

    @Test
    public void test2() {
        assert !isHappy(2);
    }
}
